#ifndef __LOAD_H
#define __LOAD_H

#include <stdint.h>

#define SYMBLE_TYPE_DATA	0x01
#define SYMBLE_TYPE_FUNC	0x02

#define ROM_HEAD_MAGIC		0xEEDD
#define SYMBLE_MAGIC		0xCC

#define LOAD_ERR_NOT_FIND_HEAD			0x01
#define LOAD_ERR_NOT_FIND_EXT_SYM		0x02

struct rom_head_t {
	uint16_t magic;
	uint16_t place;
	void* entry_addr;
	uint32_t ref_sym_addr;
	uint32_t ext_sym_addr;
};

struct sym_type_t {
	uint8_t magic;
	char name[26];
	uint8_t type;
	void* addr;
};

struct module_t {
	const struct rom_head_t* head;
	const struct sym_type_t* ext;
	struct sym_type_t* ref;
	
	uint16_t ext_sym_size;
	uint16_t ref_sym_size;
};

#define EXT_SYMBLE_FUNC_CALL(func)	\
	__attribute__((used)) __attribute__((section("EXT_SYMBLE.1"))) const struct sym_type_t ext_sym_##func = {	\
		.magic = SYMBLE_MAGIC,		\
		.name = #func,				\
		.type = SYMBLE_TYPE_FUNC,	\
		.addr = &func,				\
	};

int load_module(struct module_t* handle, uint32_t addr);

#endif
